package leetcode.offer;

import leetcode.base.Node;

/**
 * ClassName: Offer36_TreeToDoubleList
 * Description: leetcode-剑指offer 36 二叉树与双向链表
 * date: 2021-11-10 17:58
 *
 * @author liyifan
 */
public class Offer36_TreeToDoubleList {

    Node pre;

    Node head;

    public Node treeToDoublyList(Node root) {
        if (root == null)
            return null;
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    public void dfs(Node cur){
        if (cur == null)
            return;
        dfs(cur.left);
        if (pre != null) {
            pre.right = cur;
        } else {
            head = cur;
        }
        cur.left = pre;
        pre = cur;
        dfs(cur.right);
    }
}